
package com.woodblockwithoutco.remotecontrollerexample;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.SystemClock;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;

public class FakeMusicController {
    private static final String TAG = FakeMusicController.class.getSimpleName();

    private final Context mContext;
    private boolean mBelowKitKat;
    private Object mInstanceAudioService;
    private Method mMethodDispatchMediaKeyEvent;

    public FakeMusicController(Context context)
    {
        mContext = context;
//        f();
    }

    private void sendKeyEvent(int keyCode)
    {
        if ((this.mMethodDispatchMediaKeyEvent != null) && (this.mInstanceAudioService != null))
        {
            c(keyCode);
            return;
        }
        Log.d(TAG, "control music1");
        sendBroadcastButton(keyCode);
    }

    private void sendBroadcastButton(int keyCode) {
        if (mContext != null) {
            String packageName = getMediaButtonReceiverPackageName();
            Log.d(TAG, "getMediaButtonReceiverPackageName: " + packageName);

            long eventtime = SystemClock.uptimeMillis();
            Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
            keyIntent.setPackage(packageName);
            KeyEvent keyEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, keyCode, 0);
            keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
            mContext.sendOrderedBroadcast(keyIntent, null);
            keyEvent = KeyEvent.changeAction(keyEvent, KeyEvent.ACTION_UP);
            keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
            mContext.sendOrderedBroadcast(keyIntent, null);
        }
    }

    private void c(int paramInt)
    {
        try
        {
            Method localMethod1 = this.mMethodDispatchMediaKeyEvent;
            Object localObject1 = this.mInstanceAudioService;
            Object[] arrayOfObject1 = new Object[1];
            arrayOfObject1[0] = new KeyEvent(65535L, 200L, 0, paramInt, 0, 0, 248, 226, 8);
            localMethod1.invoke(localObject1, arrayOfObject1);
            Thread.sleep(200L);
            Method localMethod2 = this.mMethodDispatchMediaKeyEvent;
            Object localObject2 = this.mInstanceAudioService;
            Object[] arrayOfObject2 = new Object[1];
            arrayOfObject2[0] = new KeyEvent(65535L, 200L, 1, paramInt, 0, 0, 248, 0, 8);
            localMethod2.invoke(localObject2, arrayOfObject2);
            return;
        } catch (IllegalAccessException localIllegalAccessException)
        {
            localIllegalAccessException.printStackTrace();
            return;
        } catch (IllegalArgumentException localIllegalArgumentException)
        {
            localIllegalArgumentException.printStackTrace();
            return;
        } catch (InvocationTargetException localInvocationTargetException)
        {
            localInvocationTargetException.printStackTrace();
            return;
        } catch (InterruptedException localInterruptedException)
        {
            localInterruptedException.printStackTrace();
        }
    }

//    private void f()
//    {
//        mBelowKitKat = belowKitKat();
//        Class<?> clazz = ((AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE)).getClass();
//        try
//        {
//            Field fieldsService = clazz.getDeclaredField("sService");
//            fieldsService.setAccessible(true);
//            this.mInstanceAudioService = fieldsService.get(null);
//            this.mMethodDispatchMediaKeyEvent = IAudioService.class.getDeclaredMethod("dispatchMediaKeyEvent", new Class[] {
//                KeyEvent.class
//            });
//            return;
//        } catch (NoSuchFieldException e)
//        {
//            e.printStackTrace();
//            return;
//        } catch (IllegalAccessException e)
//        {
//            e.printStackTrace();
//            return;
//        } catch (IllegalArgumentException e)
//        {
//            e.printStackTrace();
//            return;
//        } catch (NoSuchMethodException e)
//        {
//            e.printStackTrace();
//        }
//    }

    private boolean belowKitKat()
    {
        return Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT;
    }

    private String getMediaButtonReceiverPackageName()
    {
        String str = Settings.System.getString(mContext.getContentResolver(), "media_button_receiver");
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        
        ComponentName cn = ComponentName.unflattenFromString(str);
        if (cn == null) {
            return null;
        }

        return cn.getPackageName();
    }

    public void sendPlayEvent()
    {
        Log.d(TAG, "music play");
        sendKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY);
    }

    public void sendPauseEvent()
    {
        Log.d(TAG, "music pause");
        sendKeyEvent(KeyEvent.KEYCODE_MEDIA_PAUSE);
    }

    public void sendPreviousEvent()
    {
        Log.d(TAG, "music previous");
        sendKeyEvent(KeyEvent.KEYCODE_MEDIA_PREVIOUS);
    }

    public void sendNextEvent()
    {
        Log.d(TAG, "music next");
        sendKeyEvent(KeyEvent.KEYCODE_MEDIA_NEXT);
    }
}
